package cn.hinglo.permission.controller;

import cn.hinglo.business.service.UserInfoService;
import cn.hinglo.common.annotation.IgnorePermission;
import cn.hinglo.common.annotation.IgnoreToken;
import cn.hinglo.common.annotation.RequestMapping;
import cn.hinglo.common.cache.SelfCacheKit;
import cn.hinglo.common.config.base.BaseController;
import cn.hinglo.common.constant.SystemConstant;
import cn.hinglo.common.interceptors.request.Post;
import cn.hinglo.common.interceptors.request.Put;
import cn.hinglo.common.util.JwtKit;
import cn.hinglo.permission.model.User;
import cn.hinglo.permission.service.LoginService;
import cn.hutool.core.util.StrUtil;
import com.jfinal.aop.Before;
import com.jfinal.aop.Inject;
import com.jfinal.plugin.activerecord.Record;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * @author HingLo
 * @createTime: 2020/04/22 15:10
 * @description: 登录相关
 */
@RequestMapping("/u")
@IgnorePermission
public class LoginController extends BaseController {

    @Inject
    private LoginService loginService;

    @Inject
    private UserInfoService userInfoService;


    /**
     * 所有系统中的用户使用此接口进行登录到后台进行管理,注意:按照规范,password字段不应再代码中出现注释
     *
     * @param username 用户名
     * @return 返回结果
     */
    @Before(Post.class)
    @IgnoreToken
    public void login(String username, String password) {
        User user = loginService.loginMethodService(username, password);
        if (user == null) {
            error("账号或者密码错误");
        } else {
            // 查找菜单
            String token = JwtKit.createJwt(user.getId().toString(), user.toString());
            getResponse().setHeader(SystemConstant.AUTHORIZATION, token);
            Map<String, Object> map = new HashMap<>(2);
            Record permission = loginService.findPermissionService(user.getId());
            map.put("permission", permission);
            map.put("menu", loginService.findMenuService(user.getId()));
            map.put("userInfo", userInfoService.getUserFieldService(user.getId()));
            // 缓存最新的权限信息
            List<String> permissionList = new ArrayList<>();
            permission.getColumns().forEach((x, y) -> permissionList.add(y.toString()));
            SelfCacheKit.put(SystemConstant.PERMISSION, user.getId().toString(), permissionList);
            success(map);
        }
    }

    /***
     * 修改密码(所有用户都有此功能,无需要权限)
     *
     * @param newPassword 新密码
     */
    @Before(Put.class)
    public void updatePassword(String password, String newPassword) {
        String userId = getAttr(SystemConstant.PERMISSION_USER_ID);
        renderJson(loginService.updatePassword(userId, password, newPassword));
    }

    /***
     * (管理员)重置密码
     * @param userId 指定的用户密码
     * @param password 密码
     */
    @Before(Put.class)
    public void restPassword(String userId, String password) {
        if (StrUtil.isBlank(userId)) {
            error("指定用户不存在,无法重置密码");
            return;
        }
        renderJson(loginService.restPassword(userId, password));
    }
}
